package com.dys.hejiayuncommunity.common.core.domain;

import java.io.Serializable;

/**
 * @className: com.dys.hejiayuncommunity.common.core.domain.BaseResponse
 * @description: 响应结果封装对象
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/12/02 21:56
 */
public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 响应状态码
     */
    private int code;
    /**
     * 响应结果描述
     */
    private String msg;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回的数据
     */
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    /**
     * 响应成功的描述
     *
     * @param data 响应成功的数据
     * @param <T>  参数类型
     * @return 响应成功返回的报文对象
     */
    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(Integer.parseInt(ResultCode.SUCCESS.getCode()));
        response.setMsg(ResultCode.SUCCESS.getMessage());
        response.setSuccess(true);
        response.setData(data);
        return response;
    }

    /**
     * 响应失败
     *
     * @param message 失败描述消息
     * @param <T>     返回的参数类型
     * @return 响应失败的报文
     */
    public static <T> BaseResponse<T> failed(String message) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(Integer.parseInt(ResultCode.ERROR.getCode()));
        response.setMsg(message);
        return response;
    }

    public static <T> BaseResponse<T> failed(String code, String message) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(Integer.parseInt(code));
        response.setMsg(message);
        return response;
    }

    /**
     * 失败返回三个参数
     */
    public static <T> BaseResponse<T> failed(String code, String message, boolean success) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(Integer.parseInt(code));
        response.setMsg(message);
        response.setSuccess(success);
        return response;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
